package com.jinlingxiu.app.mvp.presenter

import android.media.MediaPlayer
import android.media.MediaRecorder
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.Utils
import com.jinlingxiu.app.R
import com.jinlingxiu.app.mvp.contract.MainContract
import com.jinlingxiu.app.other.bean.ErrorBean
import com.jinlingxiu.app.other.bean.LinkedInitialBean
import com.jinlingxiu.app.other.bean.QuestionBean
import com.xdja.easymvp.mvp.BasePresenter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import org.yaml.snakeyaml.Yaml

/**
 * 描述:
 * Create by yuanwanli
 * Date 2020/09/08
 */
class MainPresenter(model: MainContract.Model, rootView: MainContract.View) :
    BasePresenter<MainContract.Model, MainContract.View>(model, rootView) {
    private var mediaRecorder: MediaRecorder? = null
    private var soundPath: String? = null
    fun getQuestion() {
        launch {
            val linkedInitialBean = withContext(Dispatchers.IO) {
                val yaml = Yaml()
                val assets = Utils.getApp().resources.assets
                val inputStream = assets.open("local.yml")
                var bean: QuestionBean? = null
                inputStream.use {
                    bean = yaml.loadAs(it, QuestionBean::class.java)
                }
//                assets.close()
                val questions = bean!!.questions
                var head = LinkedInitialBean()
                var temp = head
                for ((soundBeanIndex, soundBean) in questions.withIndex()) {
                    val soundType = soundBean.soundType
                    val tongueTwister = soundBean.tongueTwister
                    val initials = soundBean.initials
                    for ((initialBeanIndex, initialBean) in initials.withIndex()) {
                        if (soundBeanIndex == 0 && initialBeanIndex == 0) {
                            head.soundType = soundType
                            head.tongueTwister = tongueTwister
                            head.soundWords = initialBean.soundWords
                            head.soundInitial = initialBean.soundInitial
                            head.preInitialBean = null
                        } else {
                            var linkedInitialBean = LinkedInitialBean()
                            linkedInitialBean.soundType = soundType
                            linkedInitialBean.tongueTwister = tongueTwister
                            linkedInitialBean.soundWords = initialBean.soundWords
                            linkedInitialBean.soundInitial = initialBean.soundInitial
                            temp.nextInitialBean = linkedInitialBean
                            linkedInitialBean.preInitialBean = temp
                            temp = linkedInitialBean
                        }
                    }
                }
                head
            }
            mRootView?.showQuestions(linkedInitialBean)
        }
    }

    fun jumpNoPass(errors: ArrayList<ErrorBean>) {
        launch {
            delay(8000)
            mRootView?.jumpNoPass(errors)
        }
    }

    fun jumpPass() {
        launch {
            delay(8000)
            mRootView?.jumpPass()
        }
    }

    //录制十秒录音
    fun recordVoice() {
        launch {
            //创建录制音频对象
            val soundPath = Utils.getApp().cacheDir.absolutePath + TimeUtils.getNowMills() + ".amr"
            FileUtils.createFileByDeleteOldFile(soundPath)
            mediaRecorder = MediaRecorder()
            //设置录音来源为麦克风
            mediaRecorder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
            mediaRecorder!!.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT)
            mediaRecorder!!.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT)
            mediaRecorder!!.setOutputFile(soundPath)
            mediaRecorder!!.prepare()
            mediaRecorder!!.start()
        }
    }

    //停止录音
    fun stopRecord() {
        mediaRecorder?.stop()
        mediaRecorder?.release()
        if (soundPath == null) {
            soundPath = ""
        }
        mRootView?.recordFinish(soundPath!!)
    }

    //播放滴的轰鸣声
    fun playDiVoice() {
        launch {
            val mediaPlayer = MediaPlayer.create(Utils.getApp(), R.raw.di)
            mediaPlayer.start()
            mediaPlayer.setOnCompletionListener {
                mRootView?.playDiCompletion()
            }
        }
    }
}
